package com.itheima.prize.api.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.prize.commons.config.RedisKeys;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.mapper.CardUserMapper;
import com.itheima.prize.commons.db.service.CardUserService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PasswordUtil;
import com.itheima.prize.commons.utils.RedisUtil;
import com.mysql.cj.protocol.x.Notice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping(value = "/api")
@Api(tags = {"登录模块"})
public class LoginController {
    @Autowired
    private CardUserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="account",value = "用户名",required = true),
            @ApiImplicitParam(name="password",value = "密码",required = true)
    })
    public ApiResult login(HttpServletRequest request, @RequestParam String account,@RequestParam String password) {
        //TODO：任务3.1-登录模块-用户登录
        //查看登录次数并作非空判断
        if (redisUtil.get(account+"的登录次数")!=null && redisUtil.get(account+"的登录次数").equals(5) ){
            return new ApiResult(0,"密码错误五次，请五分钟后再登录",null);
        };
        CardUser user=userService.login(account,password);
        if(user==null){  //登录失败
            //计数器
            redisUtil.incr(account+"的登录次数",1);
            redisUtil.expire(account+"的登录次数",300);

            return new ApiResult(0,"账户名或密码错误",null);
        }
        //登录成功
        redisUtil.del(account+"的登录次数");
        request.getSession().setAttribute("user",user);  // 往session中存储数据
        return new ApiResult(1,"登录成功",user);
    }
    @GetMapping("/logout")
    @ApiOperation(value = "退出")
    public ApiResult logout(HttpServletRequest request) {
        //TODO：任务3.2-登录模块-用户退出
       /* request.getSession().removeAttribute("user");*///这样做的话是仅仅清处用户信息，但不会销毁session会话对象
        request.getSession().invalidate(); //这样做才是销毁整个会话！

        return new ApiResult(1,"退出成功",null);
    }

}
/*//查看登录次数并作非空判断
        if (redisUtil.get(account+"的登录次数")!=null && redisUtil.get(account+"的登录次数").equals(5) ){
                return new ApiResult(0,"密码错误五次，请五分钟后再登录",null);
                };
                CardUser user=userService.login(account,password);
                if(user==null){  //登录失败
                //第一次登录失败
                if(redisUtil.get(account+"的登录次数")==null){

                redisUtil.set(account+"的登录次数",0,300);
                }

                //第五次登录失败
                if (redisUtil.get(account+"的登录次数").equals(5)){

                redisUtil.expire(account+"的登录次数",300);
                return new ApiResult(0,"密码错误五次，请五分钟后再登录",null);
                };

                //计数器
                redisUtil.incr(account+"的登录次数",1);


                return new ApiResult(0,"账户名或密码错误",null);
                }
                //登录成功
                redisUtil.del(account+"的登录次数");
                request.getSession().setAttribute("user",user);  // 往session中存储数据
                return new ApiResult(1,"登录成功",user);
                }
/*/

